# coding=utf-8

from common.libs.NumberLibs import is_positive_int
from app.result.api.safebox import *

from app.dao.api.safebox import get_list as get_safebox_list, save, take
from app.dao.api.user import all_info


def show_service(token):
    try:
        result = all_info(token)
        if not result:
            return show_result(ShowCode.ERROR)
        safebox_list = get_safebox_list(token)
        result["safebox_list"] = safebox_list
        return show_result(ShowCode.SUCCESS, result)
    except Exception as e:
        print(e)
        return show_result(ShowCode.ERROR)


def save_service(token, num):
    if not is_positive_int(num):
        return save_result(SaveCode.ERROR)
    result_check = save(token, num)
    if not result_check:
        return save_result(SaveCode.ERROR)

    try:
        result = all_info(token)
        if not result:
            return save_result(SaveCode.ERROR)
        safebox_list = get_safebox_list(token)
        result["safebox_list"] = safebox_list
        return save_result(SaveCode.SUCCESS, result)
    except Exception as e:
        print(e)
        return save_result(SaveCode.ERROR)


def take_service(token, num):
    if not is_positive_int(num):
        return take_result(TakeCode.ERROR)
    result_check = take(token, num)
    if not result_check:
        return take_result(TakeCode.ERROR)
    try:
        result = all_info(token)
        if not result:
            return take_result(TakeCode.ERROR)
        safebox_list = get_safebox_list(token)
        result["safebox_list"] = safebox_list
        return take_result(TakeCode.SUCCESS, result)
    except Exception as e:
        print(e)
        return take_result(TakeCode.ERROR)
